package snippets;

/*
 * Browser example snippet: query DOM node value (pre-eclipse/swt 3.5 implementation)
 * An improved implementation of this snippet that can be used with eclipse/swt 3.5
 * and newer is available at
 * http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet308.java?view=co
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class EmbeddedBrowserWLogin {
	public static void main(String [] args) throws InterruptedException {
//		final String html = "<html><title>Snippet</title><body><p id='username'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Browser browser;
		try {
			browser = new Browser(shell, SWT.NONE);
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " + e.getMessage());
			return;
		}
		/* Load an HTML document */
//		browser.setText(html);
		login(browser, "http://10.8.1.13", "admin", "admin");
		final String[] urls = { "http://10.8.1.13/cm/dm/switch.dxml","http://10.8.1.13/perf/performance.dxml?method=getg82615min"};

		shell.open();
	
		new Thread() {
			public void run() {
				for (int i=0; i<6; i++) {
					try {Thread.sleep (2000);} catch (Throwable th) {}
					System.out.printf("%s%n",i);
					if (display.isDisposed()) return;
					final int j = i % 2;
					display.asyncExec(new Runnable() {
						public void run() {
							if (browser.isDisposed ()) return;
							browser.setUrl(urls[j]);
						}
					});
				}
			}
		}.start();

		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	private static void login(final Browser browser, String url, final String user,	final String passwd) {
		browser.addProgressListener(new ProgressListener() {
			public void changed(ProgressEvent event) {
			}
			public void completed(ProgressEvent event) {
				// we will just invoke this once
				browser.removeProgressListener(this);
				boolean result = browser.execute("document.getElementById('username').value='" + user + "';");
				if (!result) {
					System.out.println("Script 1 was not executed.");
					return;
				}
				result = browser.execute("document.getElementById('password').value='" + passwd + "';");
				if (!result) {
					System.out.println("Script 2 was not executed.");
					return;
				}
				result = browser.execute("document.getElementsByTagName('input')[2].click();");
				if (!result) {
					System.out.println("Script 3 was not executed.");
					return;
				}
			}
		});
		browser.setUrl(url);
	}
	static String html = 
		"<html>\n"+
		"<head>\n"+
		"<script type=\"text/javascript\" language=\"text/javascript\">\n"+
		"<!--\n"+
		"\n"+
		"function avoidFraming()\n"+
		"{\n"+
		"	if (top.location.href != window.location.href)\n"+
		"	{\n"+
		"		top.location.href = \"login.htm\";\n"+
		"	}\n"+
		"}\n"+
		"\n"+
		"// -->\n"+
		"</script>\n"+
		"<title>Login</title>\n"+
		"</head>\n"+
		"<body onLoad=\"avoidFraming();document.getElementById('username').focus();\">\n"+
		"<form action=\"login.dxml\" method=\"post\"> \n"+
		"<table align=\"center\" style=\"font-family:arial; font-size:12; border:1px solid #000000;\" width=\"95\"> \n"+
		"  <tr>\n"+
		"    <td colspan=\"3\" align=\"center\" bgcolor=\"#ff0000\" width=\"320\"><b><font color=\"#FFFFFF\">LOGIN</font></b></td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"\n"+
		"    <td align=\"right\" width=\"62\">Username: </td><td colspan=\"2\" width=\"254\">\n"+
		"      <input type=\"text\" name=\"username\" id=\"username\" size=\"38\" maxlength=\"32\"/>\n"+
		"	</td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"    <td align=\"right\" width=\"62\">Password: </td>\n"+
		"	<td colspan=\"2\" width=\"254\">\n"+
		"      <input type=\"password\" name=\"password\" size=\"38\" maxlength=\"32\"/>\n"+
		"\n"+
		"	</td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"    <td align=\"center\" width=\"62\"> </td>\n"+
		"    <td align=\"right\" width=\"136\"> </td>\n"+
		"    <td align=\"right\" width=\"146\">\n"+
		"	  <input type=\"submit\" value=\"login\"/>\n"+
		"	</td>\n"+
		"\n"+
		"  </tr> \n"+
		"</table> \n"+
		"</form> \n"+
		"</body>\n"+
		"</html>\n";
}

